using System.Collections.Generic;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0017. 电话号码的字母组合")]
    public class No0017_LetterCombinations : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(LetterCombinations, "23",
                new List<string>
                {
                    "ad", "ae", "af",
                    "bd", "be", "bf",
                    "cd", "ce", "cf",
                });
        }

        public IList<string> LetterCombinations(string digits)
        {
            var dic = new Dictionary<char, char[]>() {
                { '2', new char[] { 'a', 'b', 'c' } },
                { '3', new char[] { 'd', 'e', 'f' } },
                { '4', new char[] { 'g', 'h', 'i' } },
                { '5', new char[] { 'j', 'k', 'l' } },
                { '6', new char[] { 'm', 'n', 'o' } },
                { '7', new char[] { 'p', 'q', 'r', 's' } },
                { '8', new char[] { 't', 'u', 'v' } },
                { '9', new char[] { 'w', 'x', 'y', 'z' } },
            };
            int n = digits.Length;
            IList<string> ToResult(char[] arr, int index)
            {
                var list = new List<string>();
                if (index >= n)
                {
                    return list;
                }
                char[] cs = dic[digits[index]];
                for (int j = 0; j < cs.Length; j++)
                {
                    arr[index] = cs[j];
                    if (index == n - 1)
                    {
                        list.Add(new string(arr));
                    }
                    else
                    {
                        var nextlevelresult = ToResult(arr, index + 1);
                        list.AddRange(nextlevelresult);
                    }
                }
                return list;
            }
            char[] arr = new char[n];
            return ToResult(arr, 0);
        }
    }
}
